﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsAPICodePack.Sensors;

namespace SensorDiagnostics
{
    public partial class MainForm : Form
    {
        SensorList<Sensor> _sensors;
        Sensor _current;

        public MainForm()
        {
            InitializeComponent();

            //
            // initialize history graphs
            //
            historyGraphX.AddDataPoint("x", -4.5, 4.5, Color.FromArgb(200, Color.Red));
            historyGraphY.AddDataPoint("y", -4.5, 4.5, Color.FromArgb(200, Color.Blue));
            historyGraphZ.AddDataPoint("z", -4.5, 4.5, Color.FromArgb(200, Color.White));

            try
            {
                //
                // initialize the sensors drop down
                //
                _sensors = SensorManager.GetSensorsByCategoryId(SensorCategories.Motion);
                foreach (Sensor s in _sensors)
                {
                    cmbMotionSensors.Items.Add(s.FriendlyName);
                    s.StateChanged += new StateChangedEventHandler(Sensor_StateChanged);
                    s.DataReportChanged += new DataReportChangedEventHandler(Sensor_DataReportChanged);
                }

                //
                // select the first sensor as the selected sensor
                //
                cmbMotionSensors.SelectedIndex = 0;
                _current = _sensors[0];
            }
            catch (Exception ex)
            {
                lblSensorState.ForeColor = Color.Red;
                lblSensorState.Text = ex.Message;
            }
        }

        void Sensor_DataReportChanged(Sensor sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new MethodInvoker(delegate() { Sensor_DataReportChanged(sender, e); }));
                return;
            }

            if (_current.SensorId == sender.SensorId)
            {
                //
                // see if this is an accelerometer
                //
                Accelerometer3D accel = _current as Accelerometer3D;
                if (accel != null)
                {
                    Acceleration3D data = accel.CurrentAcceleration;

                    //
                    // add these values to the graphs
                    //
                    historyGraphX.AddValue("x", data[AccelerationAxis.X]);
                    historyGraphY.AddValue("y", data[AccelerationAxis.Y]);
                    historyGraphZ.AddValue("z", data[AccelerationAxis.Z]);
                }
            }
        }

        void Sensor_StateChanged(Sensor sender, EventArgs e)
        {
            if (_current.SensorId == sender.SensorId)
                lblSensorState.Text = _current.State.ToString();
        }

        private void cmbMotionSensors_SelectedIndexChanged(object sender, EventArgs e)
        {
            Sensor sensor = _sensors[cmbMotionSensors.SelectedIndex];
            lblSensorState.Text = sensor.State.ToString();
            _current = sensor;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }
    }
}
